import { defineStore } from "pinia";
import RequestService from "@/service/RequestService.ts";
import { SystemMenuVO } from "@/entiy/vo/SystemMenuVO.ts";
import PromiseUtils from "@/utils/PromiseUtils.ts";
import { VxeTablePropTypes } from "vxe-table";
import { SystemMenuAddFormData } from "@/entiy/form/SystemMenuAddFormData.ts";
import { RouterService } from "@/service/RouterService.ts";
import { RouterConfigVO } from "@/entiy/vo/RouterConfigVO.ts";

export const useSystemMenuManagerStore = defineStore('useSystemMenuManagerStore', {
    state: () => {
        return {
            // 表格数据
            tableDataList: [] as SystemMenuVO[],

            // 表格的加载状态
            isLoading: false,

            // 行配置
            rowConfig: {
                isCurrent: true,
                isHover: true,
                useKey: true,
                keyField: "id"
            } as VxeTablePropTypes.RowConfig,

            // 树形表格配置
            treeConfig: {
                // 平级数据展开菜单
                transform: true,
                // 展示连接线
                showLine: true,
                // 展开全部行
                expandAll: true,
                // 展开指定行 expandRowKeys: [10053]
                // 是否使用手风琴效果，只能展开一个节点
                accordion: false,
                // 树节点 ID 字段名
                rowField: 'menuId',
                // 树节点父 ID 字段名
                parentField: 'menuParentId'
            } as VxeTablePropTypes.TreeConfig,

            // 表格编辑配置
            editConfig: {
                // 手动模式编辑
                trigger: "manual",
                // 按行模式编辑
                mode: "row",
                // 显示是否已经编辑的状态
                showStatus: false,
                // 自动清除编辑状态
                autoClear: true
            } as VxeTablePropTypes.EditConfig,

            // 是否展示新增菜单的弹窗
            isShowAddSystemMenuModel: false,

            // 新增菜单的弹窗数据
            systemMenuAddFormData: {} as SystemMenuAddFormData,

            // 路由列表
            routerConfigList: [] as Array<RouterConfigVO>
        }
    },

    getters: {
        getFilterTableDataList: (state) => {
            return state.tableDataList.filter(item => {
                // 可选链避免报错
                return item?.name !== state.systemMenuAddFormData?.name;
            })
        }
    },

    actions: {
        /**
         * 添加系统菜单
         */
        async addSystemMenu() {
            console.log("新增菜单信息: ", this.systemMenuAddFormData)
            const result = await RequestService.post<Array<SystemMenuVO>>("/add-system-menu", this.systemMenuAddFormData);
            if (result.isError) {
                return PromiseUtils.reject(result.errorMsg);
            }

            this.tableDataList = result.bizDta;
            return PromiseUtils.resolve();
        },

        /**
         * 更新系统菜单
         *
         * @param row 接口参数
         */
        async updateSystemMenu(row: SystemMenuVO) {
            const data = {
                id: row.id,
                menuParentId: row.menuParentId,
                title: row.title,
                icon: row.icon,
                routerLinkName: row.routerLinkName,
                showStatus: row.showStatus
            }

            const result = await RequestService.post<Array<SystemMenuVO>>("/update-system-menu", data);
            if (result.isError) {
                return PromiseUtils.reject(result.errorMsg);
            }

            this.tableDataList = result.bizDta;
            return PromiseUtils.resolve();
        },

        /**
         * 获取系统菜单列表
         */
        async getSystemMenuList() {
            const result = await RequestService.get<Array<SystemMenuVO>>("/query-system-menu-tree-list");
            if (result.isError) {
                return PromiseUtils.reject(result.errorMsg);
            }

            this.tableDataList = result.bizDta;
            return PromiseUtils.resolve();
        },

        /**
         * 获取路由配置列表
         */
        async getRouterConfigList() {
            const result = await RouterService.getRouterConfigList();
            if (result.isError) {
                return PromiseUtils.reject(result.errorMsg);
            }

            const dataList: Array<RouterConfigVO> | null = result.bizDta;
            if (dataList == null) {
                return PromiseUtils.reject("获取路由配置列表失败");
            }

            // 过滤出可以设置为菜单链接的路由
            this.routerConfigList = (dataList ?? []).filter(
                (item) => item.isMenuLink === 1
            );

            return PromiseUtils.resolve();
        }
    }
});
